from fastapi import FastAPI, APIRouter, Depends
from starlette.middleware.cors import CORSMiddleware
from tortoise.contrib.fastapi import register_tortoise

from api import auth, user, role, menu, system, common
from utils import auth_token

app = FastAPI()

auth_app = APIRouter(dependencies=[Depends(auth_token)])
# auth_app = APIRouter()
auth_app.include_router(user)
auth_app.include_router(role)
auth_app.include_router(menu)
auth_app.include_router(system)
auth_app.include_router(common)

app.include_router(auth)
app.include_router(auth_app)


app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

register_tortoise(
    app,
    db_url="sqlite://easy_rbac.sqlite3",
    modules={"models": ["models"]},
    generate_schemas=True,
    add_exception_handlers=True,
)


if __name__ == "__main__":
    import uvicorn

    uvicorn.run("main:app", reload=True)
